import { deleteAction, getAction, postAction, putAction } from '@/api/action'
import { filterObj, clearObj } from '@/utils/util'

export const BaseListMixin = {
  data() {
    return {
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParam: {},
      /* 数据源 */
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        page: 1,
        limit: 10,
        total: 0,
      },
      /* 表格样式 */
      layout: 'total, sizes, prev, pager, next, jumper',
      elementLoadingText: '正在加载...',
      selectRows: '',
      /* table加载状态 */
      listLoading: false,
      /* 模态框状态 */
      dialogStatus: '',
      /* 模态框title */
      titleMap: {
        update: '编辑',
        create: '创建',
      },
      /* 模态框显示隐藏 */
      dialogVisible: false,
      /* 排序参数 */
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      headerRowClass: { background: '#eef1f6', color: '#838589' },
      /* form表单 */
      form: {},
    }
  },
  created() {
    if (!this.disableMixinCreated) {
      this.loadData()
    }
  },
  methods: {
    loadData(arg) {
      if (!this.url.listPage) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.page = 1
      }
      var params = this.getQueryParams() //查询条件
      this.listLoading = true
      getAction(this.url.listPage, params).then((res) => {
        if (res.code == 200) {
          this.dataSource = res.data.records
          this.ipagination.total = res.data.total
          this.listLoading = false
        }
      })
    },
    getQueryParams() {
      //获取查询条件
      let param = Object.assign({}, this.queryParam, this.isorter)
      param.page = this.ipagination.page
      param.limit = this.ipagination.limit
      return filterObj(param)
    },

    // 搜索条件查询
    searchQuery() {
      // this.loadData(1);
      this.loadData(this.queryParam)
    },

    // 重置查询
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },

    handleDeleteById(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { id: id }).then((res) => {
        that.loadData()
      })
    },

    // 主键是OID 的删除方法 区别是参数对象中的属性为oid
    handleDeleteOid(oid) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { oid: oid }).then((res) => {
        that.loadData()
      })
    },

    handleCreate() {
      clearObj(this.form)
      // console.log(this.form);
      this.dialogStatus = 'create'
      this.dialogVisible = true
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row)
      // todo 待时间格式化处理好之后  应将置为undefined删除
      this.form.createTime = undefined
      this.form.modifyTime = undefined
      this.dialogStatus = 'update'
      this.dialogVisible = true
    },

    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    },

    /* 提交表单自动判断是创建还是修改*/
    submitForm(form) {
      console.log(form)
      let that = this
      if (that.dialogStatus === 'create') {
        if (!this.url.create) {
          this.$message.error('请设置url.create属性!')
          return
        }
        //添加对象
        postAction(this.url.create, form)
          .then((response) => {
            if (response.code == 200) {
              this.$baseMessage(response.message, 'success')
              that.loadData(1)
            } else {
              this.$baseMessage(response.message, 'error')
            }
          })
          .catch((err) => {
            console.log(err)
          })
          .finally(() => {
            that.dialogVisible = false
            console.log('create obj finally code')
          })
      } else {
        // 修改对象
        if (!this.url.update) {
          this.$message.error('请设置url.update属性!')
          return
        }
        putAction(this.url.update, form)
          .then((response) => {
            if (response.code == 200) {
              this.$baseMessage(response.message, 'success')
              that.loadData(1)
            } else {
              this.$baseMessage(response.message, 'error')
            }
          })
          .catch((err) => {
            console.log(err)
          })
          .finally(() => {
            that.dialogVisible = false
            console.log('update obj finally code')
          })
      }
    },
    /* 分页一页多少条数据*/
    handleSizeChange(val) {
      this.ipagination.limit = val
      this.loadData()
    },
    /* 前往第几页*/
    handleCurrentChange(val) {
      this.ipagination.page = val
      this.loadData()
    },
    /* 查询条件方法*/
    queryData() {
      this.ipagination.page = 1
      this.loadData()
    },
    /* 打开对话框调用的方法 */
    handleEdit(row) {
      if (row == 'create') {
        this.$refs['edit'].showEdit()
        this.dialogStatus = 'create'
      } else {
        this.$refs['edit'].showEdit(row)
        this.dialogStatus = 'update'
      }
    },

    /* 不清楚是什么方法 */
    setSelectRows(val) {
      this.selectRows = val
    },

    /* 删除的方法 */
    handleDelete(row) {
      return new Promise((resolve, reject) => {
        //不能使用id 判断 ！！ 程序管理没有id属性只有oid属性
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            //删除单行
            this.handleDeleteById(row.id)
          })
        } else {
          reject()
        }
        resolve()
      })
    },
  },
}
